using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using HenIT.RTF;

namespace EventScavenger
{
    public partial class EventLogDetailView : Form
    {
        public delegate void NavigateToItemDelegate(EventEntry entry);
        public event NavigateToItemDelegate NavigateToItem;

        public EventLogDetailView()
        {
            InitializeComponent();
        }

        private List<EventEntry> eventCache;
        private EventEntry displayItem;

        public void ShowEventProperties(EventEntry displayItem, List<EventEntry> eventCache)
        {
            this.eventCache = eventCache;
            this.displayItem = displayItem;
            DisplayItem();

            this.TopMost = true;

            this.Show();

        }

        public void UpdateDisplayItem(EventEntry displayItem)
        {
            if ((this.displayItem != null) && !(this.displayItem.Equals(displayItem)))
            {
                this.displayItem = displayItem;
                DisplayItem();
            }
        }

        private void DisplayItem()
        {
            txtDate.Text = displayItem.TimeGenerated.ToShortDateString();
            txtTime.Text = displayItem.TimeGenerated.ToLongTimeString();
            txtType.Text = displayItem.EventTypeName;
            txtUser.Text = displayItem.UserName;
            txtComputer.Text = displayItem.MachineName;
            txtSource.Text = displayItem.SourceName;
            txtCategory.Text = displayItem.Category;
            txtEventID.Text = displayItem.EventID.ToString();
            txtDescription.Text = displayItem.Message;
            txtRepeats.Text = displayItem.Repeat.ToString();
            NavigateToItem(displayItem);
        }

        private void cmdUp_Click(object sender, EventArgs e)
        {
            if (eventCache.Count > 1)
            {
                for (int i = eventCache.Count - 1; i > 0; i--)
                {
                    if (displayItem.Equals(eventCache[i]))
                    {
                        displayItem = eventCache[i - 1];
                        DisplayItem();
                        break;
                    }
                }
            }
        }

        private void cmdDown_Click(object sender, EventArgs e)
        {
            if (eventCache.Count > 1)
            {
                for (int i = 0; i < eventCache.Count - 1; i++)
                {
                    if (displayItem.Equals(eventCache[i]))
                    {
                        displayItem = eventCache[i + 1];
                        DisplayItem();
                        break;
                    }
                }
            }
        }

        private void cmdCopy_Click(object sender, EventArgs e)
        {
            try
            {
                RTFBuilder rtfBuilder = new RTFBuilder();
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(string.Format("Date time: {0}", displayItem.TimeGenerated.ToShortDateString() + " " + displayItem.TimeGenerated.ToShortTimeString()));
                sb.AppendLine(string.Format("Type: {0}", displayItem.EventTypeName));
                sb.AppendLine(string.Format("Machine\\Log: {0}\\{1}", displayItem.MachineName, displayItem.LogName));
                sb.AppendLine(string.Format("Source: {0}", displayItem.SourceName));
                sb.AppendLine(string.Format("Event ID: {0}", displayItem.EventID));
                sb.AppendLine(string.Format("Repeats: {0}", displayItem.Repeat));
                sb.AppendLine(string.Format("User: {0}", displayItem.UserName));
                sb.AppendLine("Message");
                sb.AppendLine(displayItem.Message);

                rtfBuilder.FontStyle(FontStyle.Bold).Append("Date time: ").AppendLine(displayItem.TimeGenerated.ToShortDateString() + " " + displayItem.TimeGenerated.ToShortTimeString());
                rtfBuilder.FontStyle(FontStyle.Bold).Append("Type: ").AppendLine(displayItem.EventTypeName);
                rtfBuilder.FontStyle(FontStyle.Bold).Append("Machine\\Log: ").AppendLine(displayItem.MachineName + "\\" + displayItem.LogName);
                rtfBuilder.FontStyle(FontStyle.Bold).Append("Source: ").AppendLine(displayItem.SourceName);
                rtfBuilder.FontStyle(FontStyle.Bold).Append("Event ID: ").AppendLine(displayItem.EventID.ToString());
                rtfBuilder.FontStyle(FontStyle.Bold).Append("Repeats: ").AppendLine(displayItem.Repeat.ToString());
                rtfBuilder.FontStyle(FontStyle.Bold).Append("User: ").AppendLine(displayItem.UserName);
                rtfBuilder.FontStyle(FontStyle.Bold).Append("Message:");
                rtfBuilder.AppendPara().LineIndent(500).AppendLine(displayItem.Message);
                Clipboard.Clear();
                IDataObject clips = new DataObject();
                clips.SetData(DataFormats.Text, sb.ToString());
                clips.SetData(DataFormats.Rtf, rtfBuilder.ToString());
                Clipboard.SetDataObject(clips, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Copy", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EventLogDetailView_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.PageUp)
            {
                cmdUp_Click(sender, e);
            }
            if (e.KeyCode == Keys.PageDown)
            {
                cmdDown_Click(sender, e);
            }
            if (e.KeyCode == Keys.Escape)
            {
                Close();
            }
        }


    }
}